/*
 * 题目描述：给出二叉树的后序和中序序列，给出层序序列
 *
 * 输入： N 二叉树节点的数量
 *       后序序列
 *       中序序列
 *
 * 输出：层序序列
 *
 */

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

typedef struct BinTree *Node;
vector<int> postOrder;
vector<int> inOrder;
struct BinTree{
    Node left;
    Node right;
    int value;
};

Node createNode(int value){
    Node node = new BinTree;
    node->value = value;
    node->left = node->right = NULL;
    return node;
}

/*
 * 根据后序和中序序列生成二叉树
 * @param pStart 后序序列开始下标
 * @param pEnd 后序序列结束下标
 * @param iStart 中序序列开始下标
 * @param iEnd 中序序列结束下标
 *
 */
Node createBinTree(int pStart, int pEnd, int iStart, int iEnd){
    Node node;
    if(pStart > pEnd){
        node = NULL;
    }else{
        node = createNode(postOrder[pEnd]);
        int lSize,rSize;
        int root;
        for (int i = iStart; i <= iEnd; i++) {
            if (inOrder[i] == postOrder[pEnd]) {
                lSize = i - iStart;
                rSize = iEnd - i;
                root = i;
                break;
            }
        }
        node->left = createBinTree(pEnd - lSize - rSize, pEnd - rSize - 1, root - lSize, root - 1);
        node->right = createBinTree(pEnd - rSize, pEnd - 1, root + 1, root + rSize);
    }
    return node;
}

void levelOrder(Node tree){
    queue<Node> q;
    q.push(tree);
    while(!q.empty()){
        tree = q.front();
        q.pop();
        if(tree->left!=NULL){
            q.push(tree->left);
        }
        if(tree->right!=NULL){
            q.push(tree->right);
        }
        cout << tree->value;
        if(!q.empty()){
            cout << " ";
        }
    }
}

int main() {
    int treeSize;
    cin >> treeSize;
    int num;
    for (int i = 0; i < treeSize; i++) {
        cin >> num;
        postOrder.push_back(num);
    }
    for (int i = 0; i < treeSize; i++) {
        cin >> num;
        inOrder.push_back(num);
    }
    Node tree = createBinTree(0, treeSize - 1, 0, treeSize - 1);
    levelOrder(tree);
    return 0;
}